package com.cskaoyan.mall.utils;

import java.io.*;

public class FileUtils {
    /**
     * 读取src目录下的文件内容
     * @param filename
     * @return
     */
    public static String readFromFile(String filename) throws IOException {
        String path = FileUtils.class.getClassLoader().getResource(filename).getPath();
        FileInputStream inputStream = new FileInputStream(new File(path));
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        int length = 0;
        byte[] bytes = new byte[1024];
        while ((length = inputStream.read(bytes)) != -1){
            outputStream.write(bytes, 0, length);
        }
        return outputStream.toString("utf-8");
    }

    /**
     *
     * @param filename
     * @param content 写入到filename中的内容
     * @throws IOException
     */
    public static void writeFile(String filename, String content) throws IOException {
        String path = FileUtils.class.getClassLoader().getResource(filename).getPath();
        FileOutputStream fileOutputStream = new FileOutputStream(new File(path));
        fileOutputStream.write(content.getBytes("utf-8"));
    }
}
